package com.wcy.controller;


import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wcy.common.CommonResult;
import com.wcy.entity.City;
import com.wcy.entity.Role;
import com.wcy.entity.User;
import com.wcy.service.IUserService;
import com.gaofei.domain.FileResult;
import com.gaofei.service.FileService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @since 2021-04-30
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUserService userService;
    @Autowired
    FileService fileService;
    @RequestMapping("list")
    public PageInfo<User> list(String uphones, @RequestParam(defaultValue = "1")int pageNum, @RequestParam(defaultValue = "3")int pageSize){


        System.out.println(pageNum);
        System.out.println(pageSize);
        PageInfo<User> info = userService.queryAll(uphones, pageNum, pageSize);
        return info;
    }

    @RequestMapping("setpassword")
    public int setpassword(String password,String id){
        int i = userService.setPassword(password,id);
        return i;
    }

    @RequestMapping("add")
    public int add(@RequestBody User user){
        //获取用户密码，加密之后存入实体类
        String password = user.getPassword();
        //为密码进行md5加密,得到一个新的密码
        String newPassword = SecureUtil.md5(password);
        //将新的面存入user对象，保存到后台
        user.setPassword(newPassword);
        int i = userService.add(user);
        return i;
    }

    /**
     * 文件上传
     * @param file
     * @return
     */

    @RequestMapping("addimg")
    public CommonResult addimg(MultipartFile[] file){
        List<FileResult> fileResults = fileService.upload(file);
        return CommonResult.success(fileResults,"success");
    }

    /**
     * 三级联动查询
     * @return
     */
    @RequestMapping("queryCity")
    public List<City> queryCity(){
       List<City> cityList = userService.queryCity(1);
       return cityList;
    }

    /**
     * 删除用户
     * @param id
     * @return
     */

    @RequestMapping("delUser")
    public int delUser(String id){
        int i = userService.delUserById(id);
        return i;
    }

    /**
     * 设置角色
     * @param uid
     * @param
     * @return
     */
    @RequestMapping("setUserRole")
    public int setUserRole(int uid,@RequestBody List<Role> userRoles){
           int i = userService.delByUid(uid);
        for (Role userRole : userRoles) {
            int j =userRole.getId();
            userService.addRid(uid,j);
        }
         return 1;
    }

    /**
     * 回显
     */
    @RequestMapping("toupdate")
    public List<User> toupdate(int id){
        List<User> list = userService.selectById(id);
        return list;
    }


    /**
     * 修改
     */
     @RequestMapping("update")
     public int update(@RequestBody User user){
        System.err.println(user);
        int i = userService.updateUser(user);
      return i;
  }

    @RequestMapping("login")
    public CommonResult login(@RequestBody User user){
         //获取到登录对象的密码
        String password2  = user.getPassword();
        //对获取到的密码进行加密
        String newPassword = SecureUtil.md5(password2);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",user.getUsername());
        queryWrapper.eq("password",newPassword);
        List<User> list = userService.list(queryWrapper);
        if(list!=null&&list.size()>0){
            return CommonResult.success(list.get(0));
        }
        return CommonResult.success(null,"没有此用户");
    }


}

